﻿using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

namespace CapturePanorama.Internals
{
	// Token: 0x02000016 RID: 22
	internal class ImageEffectCopyCamera : MonoBehaviour
	{
		// Token: 0x06000092 RID: 146 RVA: 0x00009604 File Offset: 0x00007804
		public static List<ImageEffectCopyCamera.InstanceMethodPair> GenerateMethodList(Camera camToCopy)
		{
			List<ImageEffectCopyCamera.InstanceMethodPair> list = new List<ImageEffectCopyCamera.InstanceMethodPair>();
			foreach (MonoBehaviour monoBehaviour in camToCopy.gameObject.GetComponents<MonoBehaviour>())
			{
				if (monoBehaviour.enabled)
				{
					Type type = monoBehaviour.GetType();
					MethodInfo method = type.GetMethod("OnRenderImage", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[]
					{
						typeof(RenderTexture),
						typeof(RenderTexture)
					}, null);
					if (method != null)
					{
						list.Add(new ImageEffectCopyCamera.InstanceMethodPair
						{
							Instance = monoBehaviour,
							Method = method
						});
					}
				}
			}
			return list;
		}

		// Token: 0x06000093 RID: 147 RVA: 0x000096AC File Offset: 0x000078AC
		private void OnDestroy()
		{
			for (int i = 0; i < this.temp.Length; i++)
			{
				if (this.temp[i] != null)
				{
					UnityEngine.Object.Destroy(this.temp[i]);
				}
				this.temp[i] = null;
			}
		}

		// Token: 0x06000094 RID: 148 RVA: 0x000096FC File Offset: 0x000078FC
		private void OnRenderImage(RenderTexture src, RenderTexture dest)
		{
			int num = Math.Max(src.depth, dest.depth);
			for (int i = 0; i < this.temp.Length; i++)
			{
				if (this.onRenderImageMethods.Count > i + 1)
				{
					if (this.temp[i] != null && (this.temp[i].width != dest.width || this.temp[i].height != dest.height || this.temp[i].depth != num || this.temp[i].format != dest.format))
					{
						UnityEngine.Object.Destroy(this.temp[i]);
						this.temp[i] = null;
					}
					if (this.temp[i] == null)
					{
						this.temp[i] = new RenderTexture(dest.width, dest.height, num, dest.format);
					}
				}
			}
			List<RenderTexture> list = new List<RenderTexture>();
			list.Add(src);
			for (int j = 0; j < this.onRenderImageMethods.Count - 1; j++)
			{
				list.Add((j % 2 != 0) ? this.temp[1] : this.temp[0]);
			}
			list.Add(dest);
			for (int k = 0; k < this.onRenderImageMethods.Count; k++)
			{
				this.onRenderImageMethods[k].Method.Invoke(this.onRenderImageMethods[k].Instance, new object[]
				{
					list[k],
					list[k + 1]
				});
			}
		}

		// Token: 0x0400018B RID: 395
		public List<ImageEffectCopyCamera.InstanceMethodPair> onRenderImageMethods = new List<ImageEffectCopyCamera.InstanceMethodPair>();

		// Token: 0x0400018C RID: 396
		private RenderTexture[] temp = new RenderTexture[2];

		// Token: 0x02000017 RID: 23
		public struct InstanceMethodPair
		{
			// Token: 0x0400018D RID: 397
			public object Instance;

			// Token: 0x0400018E RID: 398
			public MethodInfo Method;
		}
	}
}
